import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';

import { LoginService } from '../../service/login.service';
import { NoticeService } from '../../../../share/service/notice.service';

@Component({
  selector: 'hk-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

  userName = '15256639988';
  password = '1';

  errorMsg;

  constructor(
    private loginService: LoginService,
    private noticeService: NoticeService,
    private route: ActivatedRoute,
    private router: Router) {
  }

  ngOnInit() {
    this.route.params.subscribe((params: Params) => {
      if (!params['type']) {
        this.loginService.autoLogin(msg => {
          if (msg.success) {
            this.success();
          }
        });
      }
    });
  }

  /**
   * 登录方法
   */
  login() {
    this.loginService.login(this.userName, this.password, response => {
      if (response.success) {
        this.success();
      } else {
        this.errorMsg = response.msg || '系统错误，请联系管理员';
      }
    });
  }

  private success() {
    let redirectUrl = this.loginService.redirectUrl ? this.loginService.redirectUrl : '/main/home';
    this.loginService.redirectUrl = redirectUrl;
    this.router.navigate([redirectUrl]);
  }

}
